package com.s69.appback.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.s69.appback.dto.AppInfoDto;
import com.s69.appback.entity.AppInfo;
import com.s69.appback.entity.AppVersion;
import com.s69.appback.mapper.AppInfoDao;
import com.s69.appback.mapper.AppVersionDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Service
public class AppInfoService {
    @Autowired
    AppInfoDao appInfoDao;
    @Autowired
    AppVersionDao appVersionDao;

    public PageInfo<AppInfo> getPage(AppInfoDto appInfoDto, HttpServletRequest request, Integer pageNum) {
        String token = request.getHeader("token");
        String[] split = token.split("-");
        if (split[3].equals("admin")) {
            appInfoDto.setStatus(1L);
        }
        PageHelper.startPage(pageNum, 8, "id desc");
        AppInfo appInfo = new AppInfo();
        BeanUtils.copyProperties(appInfoDto, appInfo);
        List<AppInfo> list = appInfoDao.queryAllBy(appInfo);
        return new PageInfo<>(list);

    }

    public void onOff(Long id) {
        AppInfo appInfo = appInfoDao.queryById(id);
        if (appInfo.getStatus() == 4L) {
            appInfo.setStatus(5L);
        } else if (appInfo.getStatus() == 5L || appInfo.getStatus() == 2L) {
            appInfo.setStatus(4L);
        }
        appInfoDao.update(appInfo);
    }

    public Boolean validate(AppInfo appInfo) {
        if (appInfo.getId() != null) {
            //修改
            AppInfo info = appInfoDao.queryById(appInfo.getId());
            if (info.getApkname().equals(appInfo.getApkname())) {
                return true;
            }
        }
        int validate = appInfoDao.validate(appInfo.getApkname());
        if (validate > 0) {
            return false;
        }
        return true;
    }

    public void save(AppInfo appInfo) {
        appInfoDao.insert(appInfo);
    }

    public void update(AppInfo appInfo) {
        appInfoDao.update(appInfo);
    }

    public AppInfo getListById(Long id) {
        return appInfoDao.queryById(id);
    }

    public void del(Long id, HttpServletRequest request) {
        AppVersion appVersion = new AppVersion();
        appVersion.setAppid(id);
        List<AppVersion> appVersionList = appVersionDao.queryAllBy(appVersion);
        for (AppVersion version : appVersionList) {
            if (version.getDownloadlink() != null) {
                String realPath = request.getServletContext().getRealPath(version.getDownloadlink());
                File apkFile = new File(realPath);
                if (apkFile.exists()) {
                    apkFile.delete();
                }
            }
            appVersionDao.deleteById(version.getId());
        }
        AppInfo appInfo = appInfoDao.queryById(id);
        if (appInfo.getLogopicpath()!=null){
            String realPath = request.getServletContext().getRealPath(appInfo.getLogopicpath());
            File logoFile=new File(realPath);
            if (logoFile.exists()){
                logoFile.delete();
            }
        }
        appInfoDao.deleteById(id);
    }
}
